#include "createnewdistance.h"
#include "ui_createnewdistance.h"
#include "storage/sql/sqllite.h"

createNewDistance::createNewDistance(SystemController *controller, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::createNewDistance)
{
    ui->setupUi(this);
    this->systemController = controller;
    fillComboBox();
}

createNewDistance::~createNewDistance()
{
    delete ui;
}


/*!
  \fn fillComboBox
  Function which fill the Combo box with values
  */
void createNewDistance::fillComboBox()
{
    ui->commentComboBox->clear();
    QString stmt;
    stmt = "SELECT commentId, name FROM comment";
    SqlLite db;
    if (!db.connect(this->systemController->getDatabasePath()))
        this->systemController->showErrorDialog(this,  tr("Error while loading data"), tr("While loading the data an error occoured"),
                                                db.getLasterErrorMessage());
    else
    {
        if (!db.executeCommand(stmt))
            this->systemController->showErrorDialog(this,  tr("Error while loading data"), tr("While loading the data an error occoured"),
                                                    db.getLasterErrorMessage());
        else
        {
            QSqlQuery *query = db.getQuery();
            while (query->next())
            {
                int id = query->value(0).toInt();
                QString name = query->value(1).toString();
                ui->commentComboBox->addItem(name, QString::number(id));
            }
        }
    }
    db.disconnect();
    ui->commentComboBox->setCurrentIndex(-1);
}

/*!
  \fn accept
  Overwrites the accept function
  */
void createNewDistance::accept()
{
    QDialog::accept();
}
